# Normally this makefile shouldn't be called directly and we expect the output
# path to be on a certain location to fit together with the other OP-TEE
# gits and helper scripts.

include ../../scripts/common.mk
out-dir := $(call strip-trailing-slashes-and-dots,$(O))
ifeq ($(out-dir),)
$(error invalid output directory (O=$(O)))
endif

include $(TA_DEV_KIT_DIR)/host_include/conf.mk

# By default we expect optee_client exported folder to be on a certain relative
# path, but if the client specifies the OPTEE_CLIENT_EXPORT then that path will
# be used instead.
OPTEE_CLIENT_EXPORT ?= ../../../optee_client/out/export

CC		?= $(CROSS_COMPILE)gcc
CPP		?= $(CROSS_COMPILE)cpp
LD		?= $(CROSS_COMPILE)ld
AR		?= $(CROSS_COMPILE)ar
NM		?= $(CROSS_COMPILE)nm
OBJCOPY		?= $(CROSS_COMPILE)objcopy
OBJDUMP		?= $(CROSS_COMPILE)objdump
READELF		?= $(CROSS_COMPILE)readelf

# Macros to detect the targeted architecture (e.g., arm-linux-gnueabihf or
# aarch64-linux-gnu) and the corresponding bit size (32 or 64).
define cc-arch
$(shell $(1) -v 2>&1 | grep Target | sed 's/Target: \([^-]*\).*/\1/')
endef
define cc-bits
$(if $(filter arm, $(1)),32,$(if $(filter aarch64, $(1)),64,unknown-arch))
endef

# OpenSSL is used by:
# - GP tests series 8500
# - Mbed TLS test 8103
# - User/group login tests 1027 and 1028
WITH_OPENSSL ?= y
ifeq ($(WITH_OPENSSL),y)
CFLAGS += -DOPENSSL_FOUND=1
ifneq ($(OPTEE_OPENSSL_EXPORT),)
LDFLAGS += -lcrypto
CFLAGS += -I$(OPTEE_OPENSSL_EXPORT)
else #OPTEE_OPENSSL_EXPORT
CFLAGS += -I../openssl/include
ifeq ($(call cc-bits, $(call cc-arch, $(CC))),32)
LDFLAGS += ../openssl/lib/arm/libcrypto.a -ldl
else
LDFLAGS += ../openssl/lib/aarch64/libcrypto.a -ldl
endif
endif #OPTEE_OPENSSL_EXPORT
endif #require OpenSSL

srcs := regression_1000.c

ifeq ($(CFG_GP_SOCKETS),y)
srcs += regression_2000.c \
	sock_server.c \
	rand_stream.c
endif

srcs +=	adbg/src/adbg_case.c \
	adbg/src/adbg_enum.c \
	adbg/src/adbg_expect.c \
	adbg/src/adbg_log.c \
	adbg/src/adbg_run.c \
	adbg/src/security_utils_hex.c \
	aes_perf.c \
	benchmark_1000.c \
	benchmark_2000.c \
	regression_4000.c \
	regression_4100.c \
	regression_5000.c \
	regression_6000.c \
	regression_8000.c \
	regression_8100.c \
	sha_perf.c \
	stats.c \
	xtest_helpers.c \
	xtest_main.c \
	xtest_test.c \
	xtest_uuid_helpers.c

ifeq ($(CFG_SECSTOR_TA_MGMT_PTA),y)
srcs += install_ta.c
endif

ifeq ($(CFG_SECURE_DATA_PATH),y)
srcs += sdp_basic.c
endif

ifeq ($(CFG_PKCS11_TA),y)
srcs += pkcs11_1000.c

OPTEE_PKCS11_TA_HEADERS ?= ../../../optee_client/libckteec/include
CFLAGS += -DCFG_PKCS11_TA
CFLAGS += -I$(OPTEE_PKCS11_TA_HEADERS)
LDFLAGS += -L$(OPTEE_CLIENT_EXPORT)/lib -lckteec
endif

objs 	:= $(patsubst %.c,$(out-dir)/xtest/%.o, $(srcs))

CFLAGS += -I./
CFLAGS += -I./adbg/include
CFLAGS += -I$(out-dir)/xtest

CFLAGS += -I$(OPTEE_CLIENT_EXPORT)/include
CFLAGS += -I$(TA_DEV_KIT_DIR)/host_include

CFLAGS += -I../../ta/include
CFLAGS += -I../../ta/create_fail_test/include
CFLAGS += -I../../ta/crypt/include
CFLAGS += -I../../ta/enc_fs/include
CFLAGS += -I../../ta/os_test/include
CFLAGS += -I../../ta/rpc_test/include
CFLAGS += -I../../ta/sims/include
CFLAGS += -I../../ta/miss/include
CFLAGS += -I../../ta/sims_keepalive/include
CFLAGS += -I../../ta/storage_benchmark/include
CFLAGS += -I../../ta/concurrent/include
CFLAGS += -I../../ta/concurrent_large/include
CFLAGS += -I../../ta/sha_perf/include
CFLAGS += -I../../ta/aes_perf/include
CFLAGS += -I../../ta/socket/include
CFLAGS += -I../../ta/sdp_basic/include
CFLAGS += -I../../ta/tpm_log_test/include

TA_DIR ?= /lib/optee_armtz
CFLAGS += -DTA_DIR=\"$(TA_DIR)\"

# Include configuration file generated by OP-TEE OS (CFG_* macros)
CFLAGS += -include conf.h

CFLAGS += -Wall -Wcast-align -Werror \
	  -Werror-implicit-function-declaration -Wextra -Wfloat-equal \
	  -Wformat-nonliteral -Wformat-security -Wformat=2 -Winit-self \
	  -Wmissing-declarations -Wmissing-format-attribute \
	  -Wmissing-include-dirs \
	  -Wmissing-prototypes -Wnested-externs -Wpointer-arith \
	  -Wshadow -Wstrict-prototypes -Wswitch-default \
	  -Wwrite-strings -Wno-unused-parameter \
	  -Wno-declaration-after-statement \
	  -Wno-missing-field-initializers -Wno-format-zero-length

CFLAGS += -g3

LDFLAGS += -L$(OPTEE_CLIENT_EXPORT)/lib -lteec
LDFLAGS += -lpthread -lm

.PHONY: all
all: xtest

xtest: $(objs)
	@echo "  LD      $(out-dir)/xtest/$@"
	$(q)$(CC) -o $(out-dir)/xtest/$@ $+ $(LDFLAGS)

$(out-dir)/xtest/%.o: $(CURDIR)/%.c
	$(q)mkdir -p $(out-dir)/xtest/adbg/src
	@echo '  CC      $<'
	$(q)$(CC) $(CFLAGS) -c $< -o $@

RMDIR := rmdir --ignore-fail-on-non-empty
define rm-build-dirs
	$(q)for d in $1; do $(RMDIR) $(out-dir)/xtest/$$d 2> /dev/null; true; done
	$(q)$(RMDIR) $(out-dir)/xtest 2> /dev/null; true
	$(q)$(RMDIR) $(out-dir) 2> /dev/null; true
endef

ifeq ($(CFG_GCM_NIST_VECTORS),y)
GCM_NIST_VECTORS_DECRYPT = gcmDecrypt128 gcmDecrypt192 gcmDecrypt256
GCM_NIST_VECTORS_ENCRYPT = gcmEncryptExtIV128 gcmEncryptExtIV192 \
			   gcmEncryptExtIV256

cleanfiles += $(out-dir)/gcmtestvectors.zip
$(out-dir)/gcmtestvectors.zip:
	@echo '  DL      $@'
	$(q)curl https://csrc.nist.gov/csrc/media/projects/cryptographic-algorithm-validation-program/documents/mac/gcmtestvectors.zip -o $@

define create-nist-gcm-vectors
cleanfiles += $(out-dir)/xtest/$(1).h $(out-dir)/$(1).rsp

$(out-dir)/$(1).rsp: $(out-dir)/gcmtestvectors.zip
	@echo '  UNZIP   $$@'
	$(q)unzip -o $$< $$(notdir $$@) -d $$(dir $$@)
	$(q)touch $$@


$(out-dir)/xtest/$(1).h: $(out-dir)/$(1).rsp
	@echo '  GEN     $$@'
	$(q)../../scripts/rsp_to_gcm_test.py --inf $$< --outf $$@ --mode=$(2) \
		$(if $(filter y,$(CFG_GCM_NIST_VECTORS_LIMITED)),--limited)

$(CURDIR)/regression_4000.c: $(out-dir)/xtest/$(1).h
endef

$(foreach v, $(GCM_NIST_VECTORS_DECRYPT), $(eval $(call \
	create-nist-gcm-vectors,$v,decrypt)))
$(foreach v, $(GCM_NIST_VECTORS_ENCRYPT), $(eval $(call \
	create-nist-gcm-vectors,$v,encrypt)))
endif

define embed-file
cleanfiles += $(out-dir)/xtest/$(1).h

$(out-dir)/xtest/$(1).h: $(2)
	@echo '  GEN     $$@'
	$(q)../../scripts/file_to_c.py --inf $$< --out $$@ --name $(1)

$(CURDIR)/regression_8100.c: $(out-dir)/xtest/$(1).h
endef

$(eval $(call embed-file,regression_8100_ca_crt,../../cert/ca.crt))
$(eval $(call embed-file,regression_8100_mid_crt,../../cert/mid.crt))
$(eval $(call embed-file,regression_8100_my_crt,../../cert/my.crt))
$(eval $(call embed-file,regression_8100_my_csr,../../cert/my.csr))

.PHONY: clean
clean:
	@echo '  CLEAN $(out-dir)'
	$(q)rm -f $(out-dir)/xtest/xtest
	$(q)$(foreach obj,$(objs), rm -f $(obj))
	$(q)rm -f $(cleanfiles)
	$(call rm-build-dirs,adbg/src adbg)
